/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.actions.dispositivos;

/**
 *
 * @author Andres
 */
import com.mapping.Departamento;
import com.mapping.TipoDisp;
import com.opensymphony.xwork2.ActionSupport;
import static com.opensymphony.xwork2.Action.SUCCESS;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

@Namespace(value="/")
@Action(value="buscarTipoDispositivo", results={@Result(name="success",location="/GestionDispositivos/gestionAgregarDispositivo.jsp")})
public class BuscarTipoDispositivoAction extends ActionSupport{
    private List<TipoDisp> listaTipos;
    private List<Departamento> listaDepartamentos;
    private List<TipoDisp> listaTipoComponentes;
    
    @Override
    public String execute(){
        String consulta = "select td.IDTIPO,td.TIPO from tipo_disp td left join modelo m on (td.idTipo=m.idTipo) where m.idTipo IS NULL";
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("inventarios");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        listaTipos = (List<TipoDisp>) em.createNamedQuery("TipoDisp.findAll").getResultList();
        listaDepartamentos = (List<Departamento>) em.createNamedQuery("Departamento.findAll").getResultList();
        Query query =  em.createNativeQuery(consulta,TipoDisp.class);
        listaTipoComponentes = (List<TipoDisp>) query.getResultList();
        tx.commit();
        em.close();
        
        
        return SUCCESS;
    }

    public List<TipoDisp> getListaTipos() {
        return listaTipos;
    }

    public List<Departamento> getListaDepartamentos() {
        return listaDepartamentos;
    }

    public List<TipoDisp> getListaTipoComponentes() {
        return listaTipoComponentes;
    }
    
    
}
